package LeetCode;
//
// Created by ✎﹏ℳ๓独赏☆独醉 on 2024/4/24.
// 
public class 最长公共前缀 {
//    https://leetcode.cn/problems/longest-common-prefix/description/
    public static void main(String[] args) {
        String[] strs = {"ab", "a"};
        System.out.println(longestCommonPrefix(strs));
    }
    public static String longestCommonPrefix(String[] strs) {
        if(strs.length==0){
            return "";
        }
        StringBuilder str = new StringBuilder();
        for (int i = 0; i < strs[0].length(); i++) {
            char temp = strs[0].charAt(i);
            boolean flag = true;
            for (int j = 1; j < strs.length; j++) {
                if(i >= strs[j].length() || temp != strs[j].charAt(i)){
                    flag = false;
                    break;
                }
            }
            if(flag){
                str.append(temp);
            }else {
                break;
            }
        }
        return String.valueOf(str);
    }
}
